home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / target.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  14KB  |  528 lines

  1. /* Select target systems and architectures at runtime for GDB.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <ctype.h>
  24. #include "defs.h"
  25. #include "target.h"
  26. #include "gdbcmd.h"
  27. #include "symtab.h"
  28. #include "inferior.h"
  29. #include "bfd.h"
  30. #include "symfile.h"
  31.  
  32. extern int memory_insert_breakpoint(), memory_remove_breakpoint();
  33. extern void host_convert_to_virtual(), host_convert_from_virtual();
  34.  
  35. static void cleanup_target ();
  36.  
  37. /* Pointer to array of target architecture structures; the size of the
  38.    array; the current index into the array; the allocated size of the 
  39.    array.  */
  40. struct target_ops **target_structs;
  41. unsigned target_struct_size;
  42. unsigned target_struct_index;
  43. unsigned target_struct_allocsize;
  44. #define    DEFAULT_ALLOCSIZE    10
  45.  
  46. /* The initial current target, so that there is always a semi-valid
  47.    current target.  */
  48.  
  49. struct target_ops dummy_target = {"None", "None", "",
  50.     0, 0, 0, 0,        /* open, close, attach, detach */
  51.     0, 0,        /* resume, wait */
  52.     0, 0, 0, 0, 0,    /* registers */
  53.     0, 0,         /* memory */
  54.     0, 0,         /* bkpts */
  55.     0, 0, 0, 0, 0,     /* terminal */
  56.     0, 0,         /* kill, load */
  57.     0, 0,         /* call_function, lookup_symbol */
  58.     0, 0,        /* create_inferior, mourn_inferior */
  59.     dummy_stratum, 0,    /* stratum, next */
  60.     0, 0, 0, 0, 0,    /* all mem, mem, stack, regs, exec */
  61.     0, 0,        /* section pointers */
  62.     OPS_MAGIC,
  63. };
  64.  
  65. /* The target structure we are currently using to talk to a process
  66.    or file or whatever "inferior" we have.  */
  67.  
  68. struct target_ops *current_target;
  69.  
  70. /* The stack of target structures that have been pushed.  */
  71.  
  72. struct target_ops **current_target_stack;
  73.  
  74. /* Command list for target.  */
  75.  
  76. static struct cmd_list_element *targetlist = NULL;
  77.  
  78. /* The user just typed 'target' without the name of a target.  */
  79.  
  80. /* ARGSUSED */
  81. static void
  82. target_command (arg, from_tty)
  83.      char *arg;
  84.      int from_tty;
  85. {
  86.   fputs_filtered ("Argument required (target name).\n", stdout);
  87. }
  88.  
  89. /* Add a possible target architecture to the list.  */
  90.  
  91. void
  92. add_target (t)
  93.      struct target_ops *t;
  94. {
  95.   if (t->to_magic != OPS_MAGIC)
  96.     {
  97.       fprintf(stderr, "Magic number of %s target struct wrong\n", 
  98.     t->to_shortname);
  99.       abort();
  100.     }
  101.  
  102.   if (!target_structs)
  103.     {
  104.       target_struct_allocsize = DEFAULT_ALLOCSIZE;
  105.       target_structs = (struct target_ops **) xmalloc
  106.     (target_struct_allocsize * sizeof (*target_structs));
  107.     }
  108.   if (target_struct_size >= target_struct_allocsize)
  109.     {
  110.       target_struct_allocsize *= 2;
  111.       target_structs = (struct target_ops **) xrealloc (target_structs, 
  112.     target_struct_allocsize * sizeof (*target_structs));
  113.     }
  114.   target_structs[target_struct_size++] = t;
  115.   cleanup_target (t);
  116.  
  117.   if (targetlist == NULL)
  118.     add_prefix_cmd ("target", class_run, target_command,
  119.             "Connect to a target machine or process.\n\
  120. The first argument is the type or protocol of the target machine.\n\
  121. Remaining arguments are interpreted by the target protocol.  For more\n\
  122. information on the arguments for a particular protocol, type\n\
  123. `help target ' followed by the protocol name.",
  124.             &targetlist, "target ", 0, &cmdlist);
  125.   add_cmd (t->to_shortname, no_class, t->to_open, t->to_doc, &targetlist);
  126. }
  127.  
  128. /* Stub functions */
  129.  
  130. static void
  131. ignore ()
  132. {
  133. }
  134.  
  135. /* ARGSUSED */
  136. static int
  137. nomemory (memaddr, myaddr, len, write)
  138.      CORE_ADDR memaddr;
  139.      char *myaddr;
  140.      int len;
  141.      int write;
  142. {
  143.   return 0;        /* No bytes handled */
  144. }
  145.  
  146. static void
  147. tcomplain ()
  148. {
  149.   error ("You can't do that when your target is `%s'",
  150.      current_target->to_shortname);
  151. }
  152.  
  153. static int
  154. noprocess ()
  155. {
  156.   error ("You can't do that without a process to debug");
  157. }
  158.  
  159. /* ARGSUSED */
  160. static int
  161. nosymbol (name, addrp)
  162.      char *name;
  163.      CORE_ADDR *addrp;
  164. {
  165.   return 1;        /* Symbol does not exist in target env */
  166. }
  167.  
  168. /* ARGSUSED */
  169. static void
  170. default_terminal_info (args, from_tty)
  171.      char *args;
  172.      int from_tty;
  173. {
  174.   printf("No saved terminal information.\n");
  175. }
  176.  
  177. #if 0
  178. /* With strata, this function is no longer needed.  FIXME.  */
  179. /* This is the default target_create_inferior function.  It looks up
  180.    the stack for some target that cares to create inferiors, then
  181.    calls it -- or complains if not found.  */
  182.  
  183. static void
  184. upstack_create_inferior (exec, args, env)
  185.      char *exec;
  186.      char *args;
  187.      char **env;
  188. {
  189.   struct target_ops *t;
  190.  
  191.   for (t = current_target;
  192.        t;
  193.        t = t->to_next)
  194.     {
  195.       if (t->to_create_inferior != upstack_create_inferior)
  196.     {
  197.           t->to_create_inferior (exec, args, env);
  198.       return;
  199.     }
  200.  
  201.     }
  202.   tcomplain();
  203. }
  204. #endif
  205.  
  206. /* This is the default target_create_inferior and target_attach function.
  207.    If the current target is executing, it asks whether to kill it off.
  208.    If this function returns without calling error(), it has killed off
  209.    the target, and the operation should be attempted.  */
  210.  
  211. static void
  212. kill_or_be_killed (from_tty)
  213.      int from_tty;
  214. {
  215.   if (target_has_execution)
  216.     {
  217.       printf ("You are already running a program:\n");
  218.       target_files_info ();
  219.       if (query ("Kill it? ")) {
  220.     target_kill (0, from_tty);
  221.     if (target_has_execution)
  222.       error ("Killing the program did not help.");
  223.     return;
  224.       } else {
  225.     error ("Program not killed.");
  226.       }
  227.     }
  228.   tcomplain();
  229. }
  230.  
  231. static void
  232. maybe_kill_then_attach (args, from_tty)
  233.      char *args;
  234.      int from_tty;
  235. {
  236.   kill_or_be_killed (from_tty);
  237.   target_attach (args, from_tty);
  238. }
  239.  
  240. static void
  241. maybe_kill_then_create_inferior (exec, args, env)
  242.      char *exec;
  243.      char *args;
  244.      char **env;
  245. {
  246.   kill_or_be_killed (0);
  247.   target_create_inferior (exec, args, env);
  248. }
  249.  
  250. /* Clean up a target struct so it no longer has any zero pointers in it.
  251.    We default entries, at least to stubs that print error messages.  */
  252.  
  253. static void
  254. cleanup_target (t)
  255.      struct target_ops *t;
  256. {
  257.  
  258.   /* Check magic number.  If wrong, it probably means someone changed
  259.      the struct definition, but not all the places that initialize one.  */
  260.   if (t->to_magic != OPS_MAGIC)
  261.     {
  262.       fprintf(stderr, "Magic number of %s target struct wrong\n", 
  263.     t->to_shortname);
  264.       abort();
  265.     }
  266.  
  267. #define de_fault(field, value) \
  268.   if (!t->field)    t->field = value
  269.  
  270.   /*        FIELD            DEFAULT VALUE        */
  271.  
  272.   de_fault (to_open,             tcomplain);
  273.   de_fault (to_close,             (void (*)())ignore);
  274.   de_fault (to_attach,             maybe_kill_then_attach);
  275.   de_fault (to_detach,             (void (*)())ignore);
  276.   de_fault (to_resume,             (void (*)())noprocess);
  277.   de_fault (to_wait,             noprocess);
  278.   de_fault (to_fetch_registers,     ignore);
  279.   de_fault (to_store_registers,        noprocess);
  280.   de_fault (to_prepare_to_store,    (void (*)())noprocess);
  281.   de_fault (to_convert_to_virtual,    host_convert_to_virtual);
  282.   de_fault (to_convert_from_virtual,    host_convert_from_virtual);
  283.   de_fault (to_xfer_memory,        nomemory);
  284.   de_fault (to_files_info,        ignore);
  285.   de_fault (to_insert_breakpoint,    memory_insert_breakpoint);
  286.   de_fault (to_remove_breakpoint,    memory_remove_breakpoint);
  287.   de_fault (to_terminal_init,        ignore);
  288.   de_fault (to_terminal_inferior,    ignore);
  289.   de_fault (to_terminal_ours_for_output,ignore);
  290.   de_fault (to_terminal_ours,        ignore);
  291.   de_fault (to_terminal_info,        default_terminal_info);
  292.   de_fault (to_kill,            (void (*)())noprocess);
  293.   de_fault (to_load,            tcomplain);
  294.   de_fault (to_call_function,        (struct value *(*)())noprocess);
  295.   de_fault (to_lookup_symbol,        nosymbol);
  296.   de_fault (to_create_inferior,        maybe_kill_then_create_inferior);
  297.   de_fault (to_mourn_inferior,        (void (*)())noprocess);
  298.   de_fault (to_next,            0);
  299.   de_fault (to_has_all_memory,        0);
  300.   de_fault (to_has_memory,        0);
  301.   de_fault (to_has_stack,        0);
  302.   de_fault (to_has_registers,        0);
  303.   de_fault (to_has_execution,        0);
  304.  
  305. #undef de_fault
  306. }
  307.  
  308. /* Push a new target type into the stack of the existing target accessors,
  309.    possibly superseding some of the existing accessors.
  310.  
  311.    Result is zero if the pushed target ended up on top of the stack,
  312.    nonzero if at least one target is on top of it.
  313.  
  314.    Rather than allow an empty stack, we always have the dummy target at
  315.    the bottom stratum, so we can call the function vectors without
  316.    checking them.  */
  317.  
  318. int
  319. push_target (t)
  320.      struct target_ops *t;
  321. {
  322.   struct target_ops *st, *prev;
  323.  
  324.   for (prev = 0, st = current_target;
  325.        st;
  326.        prev = st, st = st->to_next) {
  327.     if ((int)(t->to_stratum) >= (int)(st->to_stratum))
  328.       break;
  329.   }
  330.  
  331.   while (t->to_stratum == st->to_stratum) {
  332.     /* There's already something on this stratum.  Close it off.  */
  333.     (st->to_close) (0);
  334.     if (prev)
  335.       prev->to_next = st->to_next;    /* Unchain old target_ops */
  336.     else
  337.       current_target = st->to_next;    /* Unchain first on list */
  338.     st = st->to_next;
  339.   }
  340.  
  341.   /* We have removed all targets in our stratum, now add ourself.  */
  342.   t->to_next = st;
  343.   if (prev)
  344.     prev->to_next = t;
  345.   else
  346.     current_target = t;
  347.  
  348.   cleanup_target (current_target);
  349.   return prev != 0;
  350. }
  351.  
  352. /* Remove a target_ops vector from the stack, wherever it may be. 
  353.    Return how many times it was removed (0 or 1 unless bug).  */
  354.  
  355. int
  356. unpush_target (t)
  357.      struct target_ops *t;
  358. {
  359.   struct target_ops *u, *v;
  360.   int result = 0;
  361.  
  362.   for (u = current_target, v = 0;
  363.        u;
  364.        v = u, u = u->to_next)
  365.     if (u == t)
  366.       {
  367.     if (v == 0)
  368.       pop_target();            /* unchain top copy */
  369.     else {
  370.       (t->to_close)(0);        /* Let it clean up */
  371.       v->to_next = t->to_next;    /* unchain middle copy */
  372.     }
  373.     result++;
  374.       }
  375.   return result;
  376. }
  377.  
  378. void
  379. pop_target ()
  380. {
  381.   (current_target->to_close)(0);    /* Let it clean up */
  382.   current_target = current_target->to_next;
  383.   if (!current_target)        /* At bottom, push dummy.  */
  384.     push_target (&dummy_target);
  385. }
  386.  
  387. /* Move memory to or from the targets.  Iterate until all of it has
  388.    been moved, if necessary.  The top target gets priority; anything
  389.    it doesn't want, is offered to the next one down, etc.  Note the
  390.    business with curlen:  if an early target says "no, but I have a
  391.    boundary overlapping this xfer" then we shorten what we offer to
  392.    the subsequent targets so the early guy will get a chance at the
  393.    tail before the subsequent ones do. 
  394.  
  395.    Result is 0 or errno value.  */
  396.  
  397. int
  398. target_read_memory (memaddr, myaddr, len)
  399.      CORE_ADDR memaddr;
  400.      char *myaddr;
  401.      int len;
  402. {
  403.   return target_xfer_memory (memaddr, myaddr, len, 0);
  404. }
  405.  
  406. int
  407. target_write_memory (memaddr, myaddr, len)
  408.      CORE_ADDR memaddr;
  409.      char *myaddr;
  410.      int len;
  411. {
  412.   return target_xfer_memory (memaddr, myaddr, len, 1);
  413. }
  414.  
  415. int
  416. target_xfer_memory (memaddr, myaddr, len, write)
  417.      CORE_ADDR memaddr;
  418.      char *myaddr;
  419.      int len;
  420.      int write;
  421. {
  422.   int curlen;
  423.   int res;
  424.   struct target_ops *t;
  425.   
  426.   /* The quick case is that the top target does it all.  */
  427.   res = current_target->to_xfer_memory
  428.             (memaddr, myaddr, len, write, current_target);
  429.   if (res == len)
  430.     return 0;
  431.  
  432.   if (res > 0)
  433.     goto bump;
  434.   /* If res <= 0 then we call it again in the loop.  Ah well.  */
  435.  
  436.   for (; len > 0;)
  437.     {
  438.       curlen = len;        /* Want to do it all */
  439.       for (t = current_target;
  440.        t;
  441.        t = t->to_has_all_memory? 0: t->to_next)
  442.     {
  443.       res = t->to_xfer_memory(memaddr, myaddr, curlen, write, t);
  444.       if (res > 0) break;    /* Handled all or part of xfer */
  445.       if (res == 0) continue;    /* Handled none */
  446.       curlen = -res;    /* Could handle once we get past res bytes */
  447.     }
  448.       if (res <= 0)
  449.     {
  450.       /* If this address is for nonexistent memory,
  451.          read zeros if reading, or do nothing if writing.  Return error. */
  452.       if (!write)
  453.         bzero (myaddr, len);
  454.       return EIO;
  455.     }
  456. bump:
  457.       memaddr += res;
  458.       myaddr  += res;
  459.       len     -= res;
  460.     }
  461.   return 0;            /* We managed to cover it all somehow. */
  462. }
  463.  
  464.  
  465. /* ARGSUSED */
  466. static void
  467. target_info (args, from_tty)
  468.      char *args;
  469.      int from_tty;
  470. {
  471.   struct target_ops *t;
  472.   int has_all_mem = 0;
  473.   
  474.   if (symfile != 0)
  475.     printf ("Symbols from \"%s\".\n", symfile);
  476.  
  477. #ifdef FILES_INFO_HOOK
  478.   if (FILES_INFO_HOOK ())
  479.     return;
  480. #endif
  481.  
  482.   for (t = current_target;
  483.        t;
  484.        t = t->to_next)
  485.     {
  486.       if ((int)(t->to_stratum) <= (int)dummy_stratum)
  487.     continue;
  488.       if (has_all_mem)
  489.     printf("\tWhile running this, gdb does not access memory from...\n");
  490.       printf("%s:\n", t->to_longname);
  491.       (t->to_files_info)(t);
  492.       has_all_mem = t->to_has_all_memory;
  493.     }
  494. }
  495.  
  496. /* This is to be called by the open routine before it does
  497.    anything.  */
  498.  
  499. void
  500. target_preopen (from_tty)
  501.      int from_tty;
  502. {
  503.   dont_repeat();
  504.  
  505.   if (target_has_execution)
  506.     {   
  507.       if (query ("A program is being debugged already.  Kill it? "))
  508.         target_kill ((char *)0, from_tty);
  509.       else
  510.         error ("Program not killed.");
  511.     }
  512. }
  513.  
  514. static char targ_desc[] = 
  515.     "Names of targets and files being debugged.\n\
  516. Shows the entire stack of targets currently in use (including the exec-file,\n\
  517. core-file, and process, if any), as well as the symbol file name.";
  518.  
  519. void
  520. _initialize_targets ()
  521. {
  522.   current_target = &dummy_target;
  523.   cleanup_target (current_target);
  524.  
  525.   add_info ("target", target_info, targ_desc);
  526.   add_info ("files", target_info, targ_desc);
  527. }
  528.